package com.hyperion.blog.service.impl;

import com.alibaba.fastjson.JSON;
import com.hyperion.blog.utils.JWTUtils;
import com.hyperion.blog.dao.mapper.SysUserMapper;
import com.hyperion.blog.dao.pojo.SysUser;
import com.hyperion.blog.service.SSOService;
import com.hyperion.blog.service.SysUserService;
import com.hyperion.blog.service.ThreadService;
import com.hyperion.blog.vo.params.LoginParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Map;
import java.util.concurrent.TimeUnit;


/**
 * @author Hyperion
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SSOServiceImpl implements SSOService {

    @Resource
    private SysUserService sysUserService;

    @Resource
    private RedisTemplate<String, String> redisTemplate;

    @Resource
    private ThreadService threadService;

    @Resource
    private SysUserMapper sysUserMapper;

    private static final String SLAT = "HyperionLR";

    @Override
    public SysUser checkToken(String token) {
        if (StringUtils.isBlank(token)) {
            return null;
        }
        Map<String, Object> stringObjectMap = JWTUtils.checkToken(token);
        if (stringObjectMap == null) {
            return null;
        }
        String userJson = redisTemplate.opsForValue().get("TOKEN_" + token);
        if (StringUtils.isBlank(userJson)) {
            return null;
        }
        return JSON.parseObject(userJson, SysUser.class);
    }

    /**
     * 用户注册
     *
     * @param loginParam 用户注册信息
     * @return 用户加密信息
     */
    @Override
    public String register(LoginParam loginParam) {
        String account = loginParam.getAccount();
        String nickname = loginParam.getNickname();
        String password = loginParam.getPassword();
        if (StringUtils.isBlank(account) || StringUtils.isBlank(nickname) || StringUtils.isBlank(password)) {
            return null;
        }
        if (sysUserService.findUserByAccount(account) != null) {
            return null;
        }
        SysUser sysUser = new SysUser();
        sysUser.setAccount(loginParam.getAccount());
        sysUser.setAdmin(1);
        sysUser.setCreateDate(System.currentTimeMillis());
        sysUser.setDeleted(0);
        sysUser.setEmail(account + "@qq.com");
        sysUser.setNickname(loginParam.getNickname());
        sysUser.setPassword(DigestUtils.md5Hex(loginParam.getPassword() + SLAT));
        sysUser.setSalt(SLAT);
        sysUserService.saveUser(sysUser);

        String token = JWTUtils.createToken(sysUser.getId(), 24 * 60 * 60);
        redisTemplate.opsForValue().set("TOKEN_" + token, JSON.toJSONString(sysUser), 1, TimeUnit.DAYS);
        return token;
    }

    /**
     * 用户登录
     *
     * @param loginParam 登陆数据
     * @return 登陆结果
     */
    @Override
    public String login(LoginParam loginParam) {
        String account = loginParam.getAccount();
        String password = loginParam.getPassword();
        if (account == null || password == null) {
            return null;
        }
        password = DigestUtils.md5Hex(password + SLAT);
        SysUser sysUser = sysUserService.findUser(account, password);
        if (sysUser == null) {
            return null;
        }
        String token = JWTUtils.createToken(sysUser.getId(), 24 * 60 * 60);
        redisTemplate.opsForValue().set("TOKEN_" + token, JSON.toJSONString(sysUser), 1, TimeUnit.DAYS);
        threadService.updateLastLogin(sysUserMapper, sysUser);
        return token;
    }

    /**
     * 用户退出登录
     *
     * @param token 用户加密数据
     */
    @Override
    public void logout(String token) {
        SysUser sysUser = checkToken(token);
        redisTemplate.delete("Token_" + token);
    }
}
